// Copyright (c) 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

/**
*@describe:
*@author wfl19/Kristas
*@date 2021/08/31
 */

package bean_factory

import (
	. "gitee.com/kristas/booting-go/framework/core/bean"
	"gitee.com/kristas/booting-go/framework/core/ioc/app_context"
)

var ac = app_context.NewAppCtx()

func GetApplicationContext() app_context.ApplicationContext {
	return ac
}

func Init() error {
	err := PrepareProcessors()
	if err != nil {
		return err
	}
	err = prepareFactoryBean()
	if err != nil {
		return err
	}
	beans := ac.GetAllBeans()
	for _, bean := range beans {
		err = CreateBean(bean)
		if err != nil {
			return err
		}
	}
	return nil
}

func prepareFactoryBean() error {
	beans := ac.GetAllBeansByInterface(new(FactoryBean))
	for _, bean := range beans {
		fb := bean.(FactoryBean)
		err := BindProperties(bean)
		if err != nil {
			return err
		}
		ac.RegisterFactoryBean(fb)
	}
	return nil
}
